/*
 * @Author: dyt
 * @Date: 2018-07-04 09:56:30
 * @Last Modified by: simpledyt
 * @Last Modified time: 2018-10-22 10:11:30
 */
// store.dispatch('getMenu')
import * as api from '@/service/api'
import {
  GET_MENU,
  GET_USER_INFO
} from './mutation-types'
import router from '@/router'
import cookie from '@/utils/cookie'
export default {
  async getMenu ({
    commit,
    state
  }) {
    // let res = await api.getLoginCode()
    commit(GET_MENU)
  },
  async getUserInfo ({
    commit
  }) {
    try {
      let res = await api.getUserInfo()
      commit(GET_USER_INFO, res.data)
      sessionStorage.setItem('me', JSON.stringify(res.data))
      return res.data
    } catch (e) {
      router.push('/index')
      console.log(e)
    }
  },
  async loginOut ({
    commit
  }) {
    try {
      let curToken = await sessionStorage.getItem('token')
      await api.loginOut({
        token: curToken
      })
      let clearMe = {
        me: {
          user: {},
          menus: []
        }
      }
      // sessionStorage.setItem('me', JSON.stringify(clearMe))
      sessionStorage.removeItem('me')
      let removeCookie = cookie.remove('device_id')
      let removeToken = sessionStorage.removeItem('token')
      commit(GET_USER_INFO, clearMe)
      if (!removeToken && !removeCookie) {
        setTimeout(() => {
          router.push('/index')
        }, 300)
      }
    } catch (e) {
      router.push('/index')
      console.log(e)
    }
  },
  async redeemByPhone ({
    commit,
    state
  }, phone) {
    try {
      await api.redeemByPhone({
        phone: phone
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async redeemByEmail ({
    commit,
    state
  }, email) {
    try {
      await api.redeemByEmail({
        email: email
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async redeemByNewEmail ({
    commit,
    state
  }, email) {
    try {
      await api.redeemByNewEmail({
        email: email
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async modifyUserPhone ({
    dispatch
  }, data) {
    try {
      await api.modifyUserPhone({
        ...data
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async modifyUserEmail ({
    dispatch
  }, data) {
    try {
      await api.modifyUserEmail({
        ...data
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async modifyUserInfo ({
    commit
  }, data) {
    try {
      await api.modifyUserInfo({
        ...data
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async modifyPassword ({
    commit
  }, data) {
    try {
      await api.modifyPassword({
        ...data
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  // 1手机，2邮箱
  async verifyCode ({
    commit,
    state
  }, data) {
    try {
      await api.verifyCode({
        ...data
      })
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async sendMessage ({
    commit,
    state
  }, data) {
    try {
      await api.sendMessage({
        ...data
      })
      commit('UPDATE_CUR_FRESH', Date.parse(new Date()))
      return true
    } catch (e) {
      console.log(e)
      return false
    }
  },
  async getDynamicRegisterCode ({
    commit,
    state
  }, phone) {
    try {
      await api.getDynamicRegisterCode({
        phone: phone
      })
      return true
    } catch (err) {
      console.log(err)
      return false
    }
  },
  linkToAccount () {
    router.push('/account/index?type=info')
  }
}
